package com.xoms.admin.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xoms.admin.controller.res.bean.RProUnitBean;
import com.xoms.admin.model.AccountBean;
import com.xoms.admin.model.ProUnitBean;
import com.xoms.admin.persist.dao.ProUnitDao;
import com.xoms.admin.service.ProUnitService;
import com.xoms.admin.service.exception.ServiceException;
import com.xoms.admin.service.res.PageResult;
import com.xoms.util.DateTimeUtil;

@Service
public class ProUnitServiceImpl implements ProUnitService {
	
	Logger logger = Logger.getLogger(this.getClass());

	@Autowired
	ProUnitDao proUnitImpl;

	@Override
	public PageResult<RProUnitBean> getUnits(AccountBean account, String key, int page, int pagesize) {
		PageResult<RProUnitBean> result = new PageResult<RProUnitBean>();
		List<RProUnitBean> resList = new ArrayList<RProUnitBean>();
		// 获取单位数量
		int count = proUnitImpl.selectCount(key);
		if (count > 0) {
			// 数量不为0时获取单位列表
			List<ProUnitBean> lists = proUnitImpl.selectList(key, page, pagesize);

			if (lists != null) {
				RProUnitBean rp = null;
				for (ProUnitBean punit : lists) {
					rp = new RProUnitBean();
					rp = parse(punit);
					resList.add(rp);
				}
			}
		}
		result.setData(resList);
		result.setCount(count);
		return result;
	}
	
	/**
	 * 格式化数据
	 * @param punit
	 * @return
	 */
	private RProUnitBean parse(ProUnitBean punit) {
		RProUnitBean res = new RProUnitBean();
		res.setAccount(punit.getAccount());
		res.setName(punit.getName());
		res.setRemark(punit.getRemark());
		res.setStatus(punit.getStatus());
		res.setUid(punit.getUid());
		res.setCreateTime(DateTimeUtil.formatTimestamp(punit.getCreateTime()));
		return res;
	}

	@Override
	public RProUnitBean edit(AccountBean account, int uid, String name, String remark, int isEnable) {
		boolean flag = false;
		ProUnitBean data = new ProUnitBean();
		data.setName(name);
		data.setRemark(remark);
		data.setStatus(isEnable);

		if (uid == -1) {// 新建
			// 新建时验证单位名称是否已存在
			ProUnitBean temp = proUnitImpl.selectOneByName(name);
			if (temp != null) {
				throw new ServiceException("unit_name_is_exist");
			}
	
			data.setAccountId(account.getAccid());
			flag = proUnitImpl.insert(data) > 0;
			if (!flag) {
				throw new ServiceException("database_insert_fail","商品单位");
			}
		} else {
			data.setUid(uid);
			flag = proUnitImpl.update(data) > 0;// 更新数据库
			if (!flag) {
				throw new ServiceException("database_update_fail","商品单位");
			}
		}
		return parse(proUnitImpl.selectOneById(data.getUid()));
	}

	@Override
	public boolean del(AccountBean account, int uid) {
		// 删除商品有无设置该单位，如有则无法删除
		int member = proUnitImpl.selectCount2Unit(uid);
		if(member > 0) {
			throw new ServiceException("unit_had_connect_to_pro");
		}
		if (proUnitImpl.del(uid) > 0) {// 删除单位
			return true;
		}
		return false;
	}

	@Override
	public boolean enable(AccountBean account, int uid) {
		if (proUnitImpl.updateStatus(uid, 1) > 0) {// 更新状态
			return true;
		}
		return false;
	}
	
	@Override
	public boolean disable(AccountBean account, int uid) {
		// 删除商品有无设置该单位，如有则无法删除
		int member = proUnitImpl.selectCount2Unit(uid);
		if(member > 0) {
			throw new ServiceException("unit_had_connect_to_pro");
		}
		if (proUnitImpl.updateStatus(uid, 0) > 0) {// 更新状态
			return true;
		}
		return false;
	}
	
}
